package com.weasel.weixin.commons.message.builder.initiative;

import com.weasel.weixin.commons.message.bean.initiative.NewsCustomInitiativeMessage;

/**
 * Created by Dylan on 2015/10/19.
 */
public class NewsCustomInitiativeMessageBuilder extends AbstractCustomInitiativeMessageBuilder<NewsCustomInitiativeMessageBuilder,NewsCustomInitiativeMessage> {


    public NewsCustomInitiativeMessageBuilder() {
        super(new NewsCustomInitiativeMessage());
    }

    public ArticleCustomInitiativeMessageBuilder addArticle(){
        return new ArticleCustomInitiativeMessageBuilder(this);
    }

    private NewsCustomInitiativeMessage.News getNews(){

        NewsCustomInitiativeMessage.News news = ((NewsCustomInitiativeMessage)message).getNews();
        if(null == news){
            news = new NewsCustomInitiativeMessage.News();
            ((NewsCustomInitiativeMessage) message).setNews(news);
        }
        return news;
    }


    public static class ArticleCustomInitiativeMessageBuilder{

        private NewsCustomInitiativeMessageBuilder parent;

        private NewsCustomInitiativeMessage.Article article = new NewsCustomInitiativeMessage.Article();

        public ArticleCustomInitiativeMessageBuilder() {
        }

        public ArticleCustomInitiativeMessageBuilder(NewsCustomInitiativeMessageBuilder parent) {
            this.parent = parent;
        }

        public ArticleCustomInitiativeMessageBuilder title(String title){

            article.setTile(title);
            return this;
        }

        public ArticleCustomInitiativeMessageBuilder description(String description){
            article.setDescription(description);
            return this;
        }

        public ArticleCustomInitiativeMessageBuilder url(String url){
            article.setUrl(url);
            return this;
        }

        public ArticleCustomInitiativeMessageBuilder picUrl(String picUrl) {
            article.setPicUrl(picUrl);
            return this;
        }

        public NewsCustomInitiativeMessageBuilder end(){

            if(null == parent){
                throw new RuntimeException("请调用带参的构造方法，并传递NewsCustomInitiativeMessageBuilder参数，否则请调用build方法");
            }
            parent.getNews().addArticle(build());
            return parent;
        }

        public NewsCustomInitiativeMessage.Article build(){
            return article;
        }
    }
}
